package com.turing.selfwebservice.common;

import java.io.Serializable;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * Title:优化的页数据控件
 * </p>
 * <p>
 * Description: 提供JSP显示所需的数据及其相关参数
 * </p>
 * <p>
 * Copyright: Copyright (c) 2003
 * </p>
 * <p>
 * Company: WESTERASOFT.COM
 * </p>
 * 
 * @author YangJianshan
 * @version 1.0 修改人:zhangbo 修改内容:1.去掉数据Collection 2.加getStartAndEndRow函数
 */

public class OptimalPageControl implements Serializable {
	public static final int ASC = 0;

	public static final int DESC = 1;
	/**
	 * 当前页号
	 */
	private int currentPageNO;
	/**
	 * 最大页号
	 */
	private int maxPageNO;
	/**
	 * 最大行数
	 */
	private int maxRowCount;
	/**
	 * 页面大小
	 */
	private int pageSize;
	/**
	 * 最小页号
	 */
	private int minPageNO;
	/**
	 * 下一页号
	 */
	private int nextPageNO;

	private int previousPageNO;

	private int startRowNOInCurrentPage;

	private int endRowNOInCurrentPage;

	public OptimalPageControl(int intPageSize, int intPageNO, int intMaxRowCount) {
		this.pageSize = intPageSize;
		this.currentPageNO = intPageNO;
		this.maxRowCount = intMaxRowCount;
		this.minPageNO = 1;
		this.maxPageNO = computeMaxPageNO(intMaxRowCount, intPageSize);
		if (this.currentPageNO > this.maxPageNO) {
			this.currentPageNO = this.maxPageNO;
		}
		this.nextPageNO = computeNextPageNO(this.currentPageNO, this.maxPageNO);
		this.previousPageNO = computePreviousPageNO(this.currentPageNO,
				this.maxPageNO);
		this.startRowNOInCurrentPage = (this.currentPageNO - 1) * this.pageSize
				+ 1;
		this.endRowNOInCurrentPage = this.currentPageNO * this.pageSize;

		if (this.pageSize >= this.maxRowCount) {
			this.currentPageNO = 1;
			this.minPageNO = 1;
			this.maxPageNO = 1;
			this.previousPageNO = 1;
			this.nextPageNO = 1;
			this.startRowNOInCurrentPage = 1;
			this.endRowNOInCurrentPage = this.maxRowCount;
		}
	}

	private int computePreviousPageNO(int intPageNO, int intMaxPageNO) {
		int intPreviousPageNO = intPageNO - 1;
		if (intPreviousPageNO < 1) {
			intPreviousPageNO = 1;
		}
		return intPreviousPageNO;
	}

	private int computeNextPageNO(int intPageNO, int intMaxPageNO) {
		int intNextPageNO = intPageNO + 1;
		if (intNextPageNO >= intMaxPageNO) {
			intNextPageNO = intMaxPageNO;
		}
		return intNextPageNO;
	}

	private int computeMaxPageNO(int intMaxRowCount, int intPageSize) {
		int intPageCount = intMaxRowCount / intPageSize;
		if (intMaxRowCount > intPageSize * intPageCount) {
			intPageCount++;
		}
		return intPageCount;
	}

	public int getCurrentPageNO() {
		return currentPageNO;
	}

	public int getMaxPageNO() {
		return maxPageNO;
	}

	public int getMaxRowCount() {
		return maxRowCount;
	}

	public int getPageSize() {
		return pageSize;
	}

	public int getMinPageNO() {
		return minPageNO;
	}

	public int getNextPageNO() {
		return nextPageNO;
	}

	public int getPreviousPageNO() {
		return previousPageNO;
	}

	public int getStartRowNOInCurrentPage() {
		return startRowNOInCurrentPage;
	}

	public int getEndRowNOInCurrentPage() {
		return endRowNOInCurrentPage;
	}

	/**
	 * 功能:由页面请求获得分页信息 参数:request 请求 pageSize 改变单页的行数,默认为20,当请求不含行数时,该参数起作用
	 * 返回值:分页信息 [0]开始行 [1]结束行 [2]单页行数 [3]请求页号
	 * */
	public static int[] getStartAndEndRow(HttpServletRequest request,
			String pageSize) {
		int intPageNo, intPageSize, intStartRowNo, intEndRowNo;
		String strPageSize, strPageNO;
		int intSortType;

		strPageSize = request.getParameter("pageSize");
		strPageNO = request.getParameter("pageNo");

		if (strPageSize == null || strPageSize.equals("")) {
			if (pageSize == null) {
				strPageSize = "20";
			} else {
				// 当页面没传页大小时,调用者可指定页大小
				strPageSize = pageSize;
			}
		}
		if (strPageNO == null || strPageNO.equals("")) {
			strPageNO = "1";
		}
		try {
			intPageNo = java.lang.Integer.parseInt(strPageNO);
			intPageSize = java.lang.Integer.parseInt(strPageSize);
		} catch (Exception e) {
			intPageNo = 1;
			intPageSize = 10;
		}
		intStartRowNo = (intPageNo - 1) * intPageSize + 1;
		intEndRowNo = intPageNo * intPageSize;
		int[] rowcount = new int[4];
		rowcount[0] = intStartRowNo;
		rowcount[1] = intEndRowNo;
		rowcount[2] = intPageSize;
		rowcount[3] = intPageNo;
		return rowcount;
	}
}